
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
# File Name: sendemail.sh
# Author: GID5564
# Description: 发送电子邮件
# Created Time: 05/01/23-06:14:31
#※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
#! /usr/bin/env bash

# 避免重复导入
#[[ -n $__XLIB_IMPORTED__MAIL ]] && return 0
#__XLIB_IMPORTED__MAIL=1

function __mail_init__() {

    export CMD_SEND="${PYLIB_PATH}/exa/mysendemail.py"
    [[ -z $(command -v python3) ]] && {
        error "请先安装 Python3 !"
        exit
    }
    export MAIL_CONFIG="${CONFIG_PATH}/.sendmail"
    if [[ ! -s ${MAIL_CONFIG} ]];then
        [[ ! -d ${CONFIG_PATH} ]] && mkdir ${CONFIG_PATH}
        __write_config_mail__
    fi
    #读取配置
    __read_config_mail__
}

function __write_config_mail__(){
    cat >>${MAIL_CONFIG} <<-'EOF'
[mail]
#for send email
host = smtp.qq.com
port = 25
#邮箱授权码
key = gumuawtbuhywbhba

EOF
}

function __read_config_mail__(){
    import ini
    declare -g m_host=$(read_ini $MAIL_CONFIG "mail" "host")
    declare -g m_port=$(read_ini $MAIL_CONFIG "mail" "port")
    declare -g m_key=$(read_ini $MAIL_CONFIG "mail" "key")
}

function send_mail(){
    [[ -z "$1" ]] && {
        error "Usage: send_mail <from> <to> <title> <content> <image> <link> <enclosure>"
        return $INVALID_ARGUMENTS
    }
    #发件人
    local from=${1}
    #收件人
    local to=${2}
    
    local ism=$(IsEMail ${from})
    local isn=$(IsEMail ${to})
    if [[ ${ism} -eq 0 && ${isn} -eq 0  ]];then
	    error "邮箱地址无效!"
	    return $INVALID_ARGUMENTS
	fi
    #邮件标题
    local title=${3}
    
    #邮件内容text/html
    local content=${4}
    local image=""
    local link=""
    if [[ -n ${content} ]];then
        #是图片
        local c_type=$(IsImage ${content}) 
        [[ ${c_type} -eq 1 ]] && {
            image=${content}
            content=""
        }
        #链接
        local c_type=$(IsLink ${content})
        [[ ${c_type} -eq 1 ]] && {
            link=${content}
            content=""
        }
    fi
    
    #附件
    local enclosure=${5}

    python3 ${CMD_SEND} ${m_host} ${m_port} ${m_key} ${FROM} ${TO} content=${content} image=${image} link=${link} enclosure=${enclosure} title=${title}
}

#发送附件
function send_enclosure(){
    local from=$1
    #多个收件人用逗号隔开
    local to=$2
    local title=$3
    #多个附件地址用逗号隔开
    local enc=$4
    local NONE=""
    send_mail ${from} ${to} ${title} ${NONE} ${NONE} ${NONE} ${enc}
}


#发送html
function send_html(){
    local from=$1
    #多个收件人用逗号隔开
    local to=$2
    local title=$3
    local html=$4
    #多个附件地址用逗号隔开
    local enc=$5
    local NONE=""
    
    send_mail ${from} ${to} ${title} ${html} ${NONE} ${NONE} ${enc}
}

#发送图像
function send_image(){
    local from=$1
    #多个收件人用逗号隔开
    local to=$2
    local title=$3
    #多个图片地址用逗号隔开
    local image=$4
    local NONE=""
    
    send_mail ${to} ${title} ${NONE} ${image} ${NONE} ${NONE}
}

#发送打印
function send_print(){
    local from=$1
    #多个收件人用逗号隔开
    local to="46djr39f@hpeprint.com"
    local title="微信远程打印资料"
    #多个附件地址用逗号隔开
    local enc=$2
    local NONE=""
    send_mail ${from} ${to} ${title} ${NONE} ${NONE} ${NONE} ${enc}
}




# 定义发送邮件的函数
#send_email "recipient@example.com" "Test Email with Attachment" "<h1>Hello, World!</h1><p>This is a test email with an attachment.</p>" "/path/to/your/attachment.txt"
function send_email_html() {
    # 参数1: 收件人邮箱地址
    # 参数2: 邮件主题
    # 参数3: 邮件正文内容（HTML格式）
    # 参数4: 附件文件路径
    recipient="$1"
    subject="$2"
    body="$3"
    attachment="$4"

    # 使用mailx命令发送邮件
    # -a：指定附件
    # -s：指定邮件主题
    # -r：指定发件人邮箱地址
    # -S：指定SMTP服务器地址和端口
    # -t：指定收件人邮箱地址
    # -A：指定附件的文件名
    # -v：显示详细信息
    # -S smtp=smtp://smtp.example.com:587 \
    # -S from="your-email@example.com" \
    # -S smtp-auth=login \
    # -S smtp-auth-user="your-email@example.com" \
    # -S smtp-auth-password="your-password" \
    mailx -a "$attachment" -s "$subject" -r "your-email@example.com" -S smtp=smtp://smtp.example.com:587 -t "$recipient" -A "$(basename "$attachment")" <<< "$body"
}


# 定义发送邮件的函数
function send_email_text() {
    # 参数1: 收件人邮箱地址
    # 参数2: 邮件主题
    # 参数3: 邮件正文内容（纯文本）
    # 参数4: 附件文件路径
    recipient="$1"
    subject="$2"
    body="$3"
    attachment="$4"

    # 使用mailx命令发送邮件
    # -s: 设置邮件主题
    # -a: 添加附件
    # -r: 设置发件人邮箱地址
    # -S: 指定SMTP服务器和端口
    # -v: 显示详细信息
    # -A: 设置附件类型，这里设置为application/octet-stream，适用于所有类型的附件
    # -o: 设置SMTP服务器认证信息，格式为"smtp_auth user password"
    # -S smtp.example.com:587: 设置SMTP服务器地址和端口
    # -r sender@example.com: 设置发件人邮箱地址
    # -A application/octet-stream: 设置附件类型
    # -o "smtp_auth user password": 设置SMTP服务器认证信息
    # -s "$subject": 设置邮件主题
    # -a "$attachment": 添加附件
    # -t: 指定收件人邮箱地址
    # -v: 显示详细信息
    mailx -S smtp=smtp://smtp.example.com:587 \
         -S from="\"Sender Name\" <sender@example.com>" \
         -S smtp-auth-user=user \
         -S smtp-auth-password=password \
         -r "\"Sender Name\" <sender@example.com>" \
         -A application/octet-stream \
         -s "$subject" \
         -a "$attachment" \
         -t "$recipient" \
         -v <<< "$body"
}

# 调用send_email函数发送邮件
#send_email "recipient@example.com" "邮件主题" "邮件正文内容" "/path/to/attachment.txt"




# 定义发送邮件的函数
function send_email_image() {
    # 参数1: 收件人邮箱地址
    # 参数2: 邮件主题
    # 参数3: 邮件正文内容
    # 参数4: 图片文件路径
    # 参数5: 附件文件路径
    recipient="$1"
    subject="$2"
    body="$3"
    image_path="$4"
    attachment_path="$5"

    # 使用mailx命令发送邮件
    # -a 指定附件
    # -s 指定邮件主题
    # -r 指定发件人邮箱地址
    # -A 指定图片附件
    # -v 显示详细信息
    # -S smtp服务器地址和端口，例如：smtp.example.com:587
    # -S smtp认证用户名和密码，例如：username:password
    # -S ssl加密连接
    mailx \
        -a "$attachment_path" \
        -s "$subject" \
        -r "your_email@example.com" \
        -A "$image_path" \
        -v \
        -S smtp=smtp.example.com:587 \
        -S smtp-auth=login \
        -S smtp-auth-user=your_email@example.com \
        -S smtp-auth-password=your_password \
        -S ssl-verify=ignore \
        "$recipient" <<EOF
From: your_email@example.com
Subject: $subject
Content-Type: multipart/mixed; boundary="boundary"
MIME-Version: 1.0

--boundary
Content-Type: text/plain

$body

--boundary
Content-Type: image/jpeg
Content-Disposition: attachment; filename="$(basename "$image_path")"
Content-Transfer-Encoding: base64

$(base64 "$image_path")

--boundary
Content-Type: application/octet-stream
Content-Disposition: attachment; filename="$(basename "$attachment_path")"
Content-Transfer-Encoding: base64

$(base64 "$attachment_path")

--boundary--
EOF
}

# 调用函数发送邮件
#send_email "recipient@example.com" "邮件主题" "邮件正文内容" "/path/to/image.jpg" "/path/to/attachment.txt"



__mail_init__













































